#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
#include <set>
#include <algorithm>
using namespace std;

const int L_BUF = (1 << 20);

const int N_CHAR = 11;
const char cset[N_CHAR + 1] = "0123456789 ";
int cidx[128];
int nNode = 0, nRecord = 0;

const int MAX_AS = (1 << 20);

class TrieNode {
public:
    list< pair<int, int> > * hijs;
    set<int> * has;
    TrieNode * sons[N_CHAR];
    TrieNode () { memset(this, 0, sizeof(TrieNode)); }
} * trieRoot = NULL;

void freeTrie(TrieNode ** rt) {
    if ((*rt) == NULL) return;
    for (int i = 0; i < N_CHAR; ++i) freeTrie(&((*rt)->sons[i]));
    if ((*rt)->hijs) {
        (*rt)->hijs->clear();
        delete (*rt)->hijs;
        (*rt)->hijs = NULL;
    }
    if ((*rt)->has) {
        (*rt)->has->clear();
        delete (*rt)->has;
        (*rt)->has = NULL;
    }
    delete *rt;
    (*rt) = NULL;
}

void initTrie() {
    for (int i = 0; cset[i]; ++i)
        cidx[(int)cset[i]] = i;
    freeTrie(&trieRoot);
    trieRoot = new TrieNode();
    nNode = 1;
    nRecord = 0;
}

int searTrie(char *s, int ptime) { // s: AS prefix, ptime: ping time
    // return 0: no, 1: yes
//    char * p = s;
    unsigned int match = 0;
    for (TrieNode * rt = trieRoot; (rt = rt->sons[cidx[(int)*s]]); ) {
        if (rt->hijs) {
//fprintf(stdout, "$$$ %s\n", p);
            match = 0;
            for (list< pair<int, int> >::iterator it = rt->hijs->begin(); it != rt->hijs->end(); ++it) {
                if (ptime >= it->first && ptime < it->second) {
                    match = 1;
                    break;
                }
            }
        }

        if (*(++s) == 0) break;
    }
    return match;
}

set<int> * searHome(char *s) { // s: prefix
    set<int> * res = NULL;
    for (TrieNode * rt = trieRoot; (rt = rt->sons[cidx[(int)*s]]); ) {
        if (rt->has) res = rt->has;
        if (*(++s) == 0) break;
    }
    return res;
}

TrieNode * insertTrie(char *s, int tb, int te) { // s: AS prefix, tb: time begin, te: time end.
    TrieNode * rt, * nxt;
    for (rt = trieRoot; *s; rt = nxt, ++s) {
        nxt = rt->sons[cidx[(int)*s]];
        if (NULL == nxt) {
            rt->sons[cidx[(int)*s]] = nxt = new TrieNode();
            ++nNode;
        }
    }

    if (rt->hijs == 0) rt->hijs = new list< pair<int, int> > ();
    rt->hijs->push_back(make_pair(tb, te));
    return rt;
}

TrieNode * insertHome(char *s, int as) { // s: AS prefix, tb: time begin, te: time end.
    TrieNode * rt, * nxt;
    for (rt = trieRoot; *s; rt = nxt, ++s) {
        nxt = rt->sons[cidx[(int)*s]];
        if (NULL == nxt) {
            rt->sons[cidx[(int)*s]] = nxt = new TrieNode();
            ++nNode;
        }
    }

    if (rt->has == 0) rt->has = new set<int> ();
    rt->has->insert(as);
    return rt;
}

char line[L_BUF], binpfx[1024], buf[1024];

int main(int argc, char ** argv)
{
    int AS;
    unsigned int ia, ib, ic, id, im;
    initTrie();

    // argv[1]: Hijacked Prefixes
    int tb, te;
    FILE * fx = fopen(argv[1], "r");
    while (fgets(line, L_BUF, fx)) {
        sscanf(line, "%u.%u.%u.%u/%u %d", &ia, &ib, &ic, &id, &im, &AS);
        if (im < 8) continue;
        ia = (((ia << 24) + (ib << 16) + (ic << 8) + id) >> (32 - im));
        binpfx[im] = 0;
        for (int i = im - 1; i >= 0; --i) { binpfx[i] = (ia & 1) + '0'; ia >>= 1; }
        sprintf(buf, "%d %s", AS, binpfx);

        int i = 0, j = 0;
        for ( ; line[i]; ++i) {
            if (line[i] == ' ') {
                ++j;
                if (j >= 4 && (j % 2) == 0) {
                    sscanf(line + i + 1, "%d %d", &tb, &te);
//fprintf(stdout, "%s %s %d %d\n", line, buf, tb, te);
                    insertTrie(buf, tb, te);
                }
            }
        }
    }
    fclose(fx);
    fprintf(stderr, "Hijacking list over.\n");

    // argv[2]: home AS
    FILE * fp = fopen(argv[2], "r");
    while (fgets(line, L_BUF, fp)) {
        sscanf(line, "%u.%u.%u.%u/%u %d", &ia, &ib, &ic, &id, &im, &AS);
        if (im < 8) continue;
        ia = (((ia << 24) + (ib << 16) + (ic << 8) + id) >> (32 - im));
        binpfx[im] = 0;
        for (int i = im - 1; i >= 0; --i) { binpfx[i] = (ia & 1) + '0'; ia >>= 1; }
        insertHome(binpfx, AS);
//fprintf(stdout, "%s %s %d\n", line, binpfx, AS);
    }
    fclose(fp);
    fprintf(stderr, "Home AS list over.\n");

    // stdin: traceroute
    // FORMAT: RES src_ip dest_ip time REASON CODE
    int ptime;
    char src[64], dest[64];
    while (fgets(line, L_BUF, stdin)) {
        sscanf(line + 2, "%s %s %d", src, dest, &ptime);

        sscanf(src, "%u.%u.%u.%u", &ia, &ib, &ic, &id);
        ia = (ia << 24) + (ib << 16) + (ic << 8) + id;
        binpfx[32] = 0;
        for (int i = 31; i >= 0; --i) { binpfx[i] = (ia & 1) + '0'; ia >>= 1; }
        set<int> * has = searHome(binpfx);
//fprintf(stdout, "%s %s %s %d %s ||", line, src, dest, ptime, binpfx);
        if (has == NULL) continue;
        else {
            int hij = 0;
            sscanf(dest, "%u.%u.%u.%u", &ia, &ib, &ic, &id);
            ia = (ia << 24) + (ib << 16) + (ic << 8) + id;
            binpfx[32] = 0;
            for (int i = 31; i >= 0; --i) { binpfx[i] = (ia & 1) + '0'; ia >>= 1; }
//fprintf(stdout, " %s ||", binpfx);
            for (set<int>::iterator it = has->begin(); it != has->end(); ++it) {
                sprintf(buf, "%d %s", *it, binpfx);
//fprintf(stdout, " %s", buf);
                if (searTrie(buf, ptime)) {
                    hij = 1;
                    break;
                }
            }
//fprintf(stdout, "\n");
            if (hij) printf("%s", line);
        }
    }
}

